<?php
/**
 * cms文章处理
 *
 *
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined('InShopNC') or exit('Access Invalid!');
class articleControl extends mobileHomeControl{

	public function __construct() {
        parent::__construct();
    }

	public function article_listOp(){
		
		 if(!empty($_GET['ac_id']) && intval($_GET['ac_id']) > 0) {
            $ac_id = " and a.ac_id in(".(int)$_GET['ac_id'].")";
         }else
		 {
			 $ac_id = " and a.ac_id in(13,14) ";
		 }
		
		if(!empty($_GET['limit']) && intval($_GET['limit']) > 0){
			$start = (int)$_GET['limit'];
		}
		else
		{
			$start = 0;
		}
	   $datas = array();
        //文章
		$num= 10;
		
		$limit = " limit ".$start.",".$num;
		
		$where  = " where 1=1 ".$ac_id;
		
        $article_list = array();
		$sql = "select a.article_id,a.ac_id,a.article_url,a.article_title,a.article_description,a.article_content,b.file_name from `".DBPRE."article` as a left join `".DBPRE."upload` as b on b.item_id = a.article_id ".$where." order by article_id desc ".$limit;

        $article_list = Db::getALl($sql);
		
		//分页
		$isMore = false;
		$sql = 'SELECT COUNT(*) as `count` FROM `'.DBPRE.'article` as a '.(isset($where) ? $where : '');
		$article_count = mysqli_fetch_array(Db::query($sql),MYSQLI_ASSOC);
		if($article_count['count']>($start+$num))
		{
			$isMore = true;
		}
		$datas['isMore'] = $isMore;
		$datas['ac_id'] = $_GET['ac_id'];
		$datas['limit'] = $start+$num;		
        $datas['article_list']=$this->article_list_extend($article_list);
        output_data($datas);
	}
	
	 public function article_list_extend($article_list)
	 {
		 header("Content-type:text/html;charset=utf-8");
		 foreach($article_list as $key=>$value)
		 {
			 
			 $article_list[$key]['article_content'] = preg_replace('/&nbsp;|&ldquo;|&rdquo;|\/r\/n/i','',trim(strip_tags($article_list[$key]['article_content'])));
			//echo($article_list[$key]['article_content']."<br/>");
			 
			 if($article_list[$key]['file_name']=="")
			 {
				 $article_list[$key]['file_name'] = "/wap/images/logo.jpg";
			 }
			 else
			 {
				$article_list[$key]['file_name'] = "/data/upload/shop/article/".$article_list[$key]['file_name'];
			 }
		 }
		 //exit();
		 return $article_list;
	 }
	 
	function GBsubstr($string, $start, $length) {  
	if(strlen($string)>$length){  
	$str=null;  
	$len=$start+$length;  
	for($i=$start;$i<$len;$i++){  
	if(ord(substr($string,$i,1))>0xa0){  
	$str.=substr($string,$i,2);  
	$i++;  
	}else{  
	$str.=substr($string,$i,1);  
	}  
	}  
	return $str;  
	}else{  
	return $string;  
	}  
	}  
	
	 public function article_detailOp()
	 { 
		 if(!empty($_GET['a_id']) && intval($_GET['a_id']) > 0) {
            $condition['a_id'] = (int)$_GET['a_id'];
         }
		 else{exit();}
		 
		 $article_model = Model("article"); 
		 $result = $article_model->getOneArticle($condition['a_id']);
		 if($result)
		 {
			 $result["article_content"] = $result["article_content"];
		 }
		 output_data($result);
	 }
	 
    /**
     * 获得推荐商品
     */
    public function get_booth_goods($gc_id=1074) {
        $isMobile=true;
        if ($gc_id <= 0) {
            return false;
        }
        // 获取分类id及其所有子集分类id
        $goods_class = H('goods_class') ? H('goods_class') : H('goods_class', true);
        if (empty($goods_class[$gc_id])) {
            return false;
        }
        $child = (!empty($goods_class[$gc_id]['child'])) ? explode(',', $goods_class[$gc_id]['child']) : array();
        $childchild = (!empty($goods_class[$gc_id]['childchild'])) ? explode(',', $goods_class[$gc_id]['childchild']) : array();
        $gcid_array = array_merge(array($gc_id), $child, $childchild);
        // 查询添加到推荐展位中的商品id
        $boothgoods_list = Model('p_booth')->getBoothGoodsList(array('gc_id' => array('in', $gcid_array)), 'goods_id', 0, $isMobile?20:4, 'rand()');
        if (empty($boothgoods_list)) {
            return false;
        }

        $goodsid_array = array();
        foreach ($boothgoods_list as $val) {
            $goodsid_array[] = $val['goods_id'];
        }

        $fieldstr = "goods_id,goods_commonid,goods_name,goods_jingle,store_id,store_name,goods_price,goods_marketprice,goods_storage,goods_image,goods_freight,goods_salenum,color_id,evaluation_count";
        $goods_list = Model('goods')->getGoodsOnlineList(array('goods_id' => array('in', $goodsid_array)), $fieldstr);
        if (empty($goods_list)) {
            return false;
        }
        $commonid_array = array();
        foreach ($goods_list as $val) {
            $commonid_array[] = $val['goods_commonid'];
        }
        $groupbuy_list = Model('groupbuy')->getGroupbuyListByGoodsCommonIDString(implode(',', $commonid_array));
        $xianshi_list = Model('p_xianshi_goods')->getXianshiGoodsListByGoodsString(implode(',', $goodsid_array));
        foreach ($goods_list as $key => $value) {
            // 团购
            if (isset($groupbuy_list[$value['goods_commonid']])) {
                $goods_list[$key]['goods_price'] = $groupbuy_list[$value['goods_commonid']]['groupbuy_price'];
                $goods_list[$key]['group_flag'] = true;
            }
            if (isset($xianshi_list[$value['goods_id']]) && !$goods_list[$key]['group_flag']) {
                $goods_list[$key]['goods_price'] = $xianshi_list[$value['goods_id']]['xianshi_price'];
                $goods_list[$key]['xianshi_flag'] = true;
            }
        }
        return compact('goods_list','groupbuy_list','xianshi_list');

    }
	
}
